/*****************************************************************************
 *  CommLayerLib for Android - Logger.java
 *  Copyright (C) 2012  Matteo Piscitelli
 *  E-mail: matteo@picciux.it
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  For further info, check http://code.google.com/p/java-comm-layer/
 *
 *  SVN: $Id: Logger.java 47 2012-10-11 17:23:13Z picciux@gmail.com $
 *  
 *****************************************************************************/

package it.picciux.commlayer.android.log;

import java.util.logging.Handler;
import java.util.logging.Level;


/**
 * @author 00688263
 *
 */
public abstract class Logger extends it.picciux.commlayer.log.Logger {
	protected java.util.logging.Logger logger;
	protected Handler handler;

	/**
	 * @param moduleName
	 */
	public Logger(String moduleName) {
		super(moduleName);
	}

	protected Level getLevelObject(int logLevel) {
		switch(logLevel) {
			case ALL:
				return Level.ALL;
			case FINEST:
				return Level.FINEST;
			case FINER:
				return Level.FINER;
			case FINE:
				return Level.FINE;
			case CONFIG:
				return Level.CONFIG;
			case INFO:
				return Level.INFO;
			case WARNING:
				return Level.WARNING;
			case SEVERE:
				return Level.SEVERE;
			default:
				return Level.OFF;
		}
	}
	
	@Override
	public boolean setLevel(int logLevel) {
		if (! super.setLevel(logLevel)) return false;
		logger.setLevel(getLevelObject(getLevel()));
		handler.setLevel(getLevelObject(getLevel()));
		return true;
	}

	@Override
	public void log(int logLevel, String msg) {
		//logger.log(getLevelObject(logLevel), msg);
		if (! isLoggable(logLevel)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(logLevel), className, methodName, msg);
	}

	@Override
	public void log(int logLevel, String msg, Object param1) {
		if (! isLoggable(logLevel)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(logLevel), className, methodName, msg, param1);
	}

	@Override
	public void log(int logLevel, String msg, Object[] params) {
		if (! isLoggable(logLevel)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(logLevel), className, methodName, msg, params);
	}

	@Override
	public void log(int logLevel, String msg, Throwable throwable) {
		if (! isLoggable(logLevel)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(logLevel), className, methodName, msg, throwable);
	}

	@Override
	public void logp(int logLevel, String className, String methodName,
			String msg) {
		logger.logp(getLevelObject(logLevel), className, methodName, msg);
	}

	@Override
	public void logp(int logLevel, String className, String methodName,
			String msg, Object param1) {
		logger.logp(getLevelObject(logLevel), className, methodName, msg, param1);
	}

	@Override
	public void logp(int logLevel, String className, String methodName,
			String msg, Object[] params) {
		logger.logp(getLevelObject(logLevel), className, methodName, msg, params);
	}

	@Override
	public void logp(int logLevel, String className, String methodName,
			String msg, Throwable throwable) {
		logger.logp(getLevelObject(logLevel), className, methodName, msg, throwable);		
	}

	@Override
	public void finest(String msg) {
		if (! isLoggable(FINEST)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(FINEST), className, methodName, msg);
	}

	@Override
	public void finer(String msg) {
		if (! isLoggable(FINER)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(FINER), className, methodName, msg);
	}

	@Override
	public void fine(String msg) {
		if (! isLoggable(FINE)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(FINE), className, methodName, msg);
	}

	@Override
	public void config(String msg) {
		if (! isLoggable(CONFIG)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(CONFIG), className, methodName, msg);
	}

	@Override
	public void info(String msg) {
		if (! isLoggable(INFO)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(INFO), className, methodName, msg);
	}

	@Override
	public void warning(String msg) {
		if (! isLoggable(WARNING)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(WARNING), className, methodName, msg);
	}

	@Override
	public void severe(String msg) {
		if (! isLoggable(SEVERE)) return;
		String className = "";
		String methodName = "";
		
		try {
			throw new Exception("backtrace");
		} catch (Exception e) {
			className = e.getStackTrace()[1].getClassName();
			methodName = e.getStackTrace()[1].getMethodName() + ":" + e.getStackTrace()[1].getLineNumber();
		}
		
		logger.logp(getLevelObject(SEVERE), className, methodName, msg);
	}

	@Override
	public void entering(String className, String methodName) {
		logger.entering(className, methodName);
	}

	@Override
	public void entering(String className, String methodName, Object param1) {
		logger.entering(className, methodName, param1);
	}

	@Override
	public void entering(String className, String methodName, Object[] params) {
		logger.entering(className, methodName, params);
	}

	@Override
	public void exiting(String className, String methodName) {
		logger.exiting(className, methodName);
	}

	@Override
	public void exiting(String className, String methodName, Object retValue) {
		logger.exiting(className, methodName, retValue);
	}

}
